.text

/* See https://www.codesourcery.com/archives/arm-gnu/msg01396.html
     and http://www.sourceware.org/ml/binutils/2005-04/msg00902.html
   This avoids unshifted register error which occurs when compiling for Thumb */
.syntax unified
.thumb

.global main
main:
    /* I have to use +1 in the address abel, otherwise the processor triggers
       an exception.  The ARM documentation explains that thumb bx
       instructions require odd addresses.  The C compiler does this
       automatically - I have examined the assembly generated by the C compiler,
       but still don't understand how they do it without manually appending
       a +1 to the address label. */
    .word   0x20000800    /* stack pointer */
    .word   _c_entry+1    /* Entry point (reset vector) */
    .rept   82            /* interrupt handlers */
    .word   _handler+1
    .endr




_c_entry:
    /* Enable clock to Port C */
    movw    r3, #4096
    movt    r3, #16386
    ldr     r2, [r3, #24]
    orr     r2, r2, #16
    str     r2, [r3, #24]

    /* Turn LED off */
    movw    r3, #4096
    movt    r3, #16385
    movw    r2, #4096
    str     r2, [r3, #16]

    ldr     r2, [r3, #4]    /* Change CRH pin 12 */
    and     r2, r2, #4293984255
    orr     r2, r2, #196608
    str     r2, [r3, #4]    /* MODE  = general purpose I/O, speed 50 Mhz */

    ldr     r2, [r3, #4]    /* Get CRH */

_flash_loop:
    /* Turn LED on */
    movw    r2, #4096
    str      r2, [r3, #20]



    movw    r4, #0
    movt    r4, #20
delay1:
    sub.w   r4, r4, #1
    cmp     r4, #0
    bne     delay1


    /* Turn LED off */
    movw    r2, #4096
    str     r2, [r3, #16]



    movw    r4, #0
    movt    r4, #3
delay2:
    sub.w   r4, r4, #1
    cmp     r4, #0
    bne     delay2



    B _flash_loop



_handler:
    bx      lr    /* Return immediately */
    nop


